package com.fuint.module.schedule;

import cn.hutool.core.collection.CollUtil;
import com.fuint.common.dto.OrderDto;
import com.fuint.common.enums.OrderStatusEnum;
import com.fuint.common.enums.PayStatusEnum;
import com.fuint.common.service.OrderService;
import com.fuint.common.util.LogUtils;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.repository.model.MtOrder;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单超时处理定时任务
 */
@EnableScheduling
@Component("orderCancelJob")
@RequiredArgsConstructor
public class OrderCancelJob {
    /**
     * 订单服务接口
     */
    private final OrderService orderService;
    private final Environment environment;
    /**
     * 一次最多处理订单数量
     **/
    private final static int MAX_SEND_NUM = 50;
    /**
     * 订单超时分钟
     */
    private final static int OVER_TIME = 30;

    @Scheduled(cron = "${orderCancel.job.time}")
    @Transactional(rollbackFor = Exception.class)
    public void dealOrder() throws BusinessCheckException {
        String theSwitch = environment.getProperty("orderCancel.job.switch");
        if (theSwitch != null && theSwitch.equals("1")) {
            LogUtils.info("OrderCancelJobStart!!!");
            Map<String, Object> param = new HashMap<>();
            param.put("status", OrderStatusEnum.CREATED.getKey());
            List<MtOrder> dataList = orderService.getOrderListByParams(param);
            if (CollUtil.isNotEmpty(dataList)) {
                int dealNum = 0;
                for (MtOrder mtOrder : dataList) {
                    Date overTime = new Date(mtOrder.getCreateTime().getTime() + (60000 * OVER_TIME));
                    Date nowTime = new Date();
                    // 超时关闭订单
                    if (dealNum <= MAX_SEND_NUM && (overTime.getTime() <= nowTime.getTime())) {
                        if (mtOrder.getPayStatus().equals(PayStatusEnum.WAIT.getKey())) {
                            orderService.cancelOrder(mtOrder.getId(), "超时未支付取消");
                            dealNum++;
                        } else if (mtOrder.getPayStatus().equals(PayStatusEnum.SUCCESS.getKey())) {
                            OrderDto reqDto = new OrderDto();
                            reqDto.setId(mtOrder.getId());
                            reqDto.setStatus(OrderStatusEnum.PAID.getKey());
                            orderService.updateOrder(reqDto);
                            orderService.setOrderPayed(mtOrder.getId(), null);
                        }
                    }
                }
            }
            LogUtils.info("OrderCancelJobEnd!!!");
        }
    }
}
